﻿using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using XamlaApp.Views.ElementViews;
using System.Windows.Media;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace XamlaApp.ViewModels
{
    [DataContract]
    public class ImageViewModel : FrameworkElementViewModel
    {
        public ImageViewModel()
        {
            //create a default placeholder for the Image
            var defaultImg = new WriteableBitmap(new EmptyImagePlaceholder(), null);
            defaultImg.Invalidate();
            ImageSource = defaultImg;

            //set the default ImageStretch
            ImageStretch = Stretch.None;
        }

        private Stretch _imageStretch;

        [DataMember]
        public Stretch ImageStretch
        {
            get { return _imageStretch; }
            set
            {
                _imageStretch = value;
                PropertyChanging("ImageStretch");
            }
        }

        private WriteableBitmap _imageSource;
        [IgnoreDataMember]
        public WriteableBitmap ImageSource
        {
            get { return _imageSource; }
            set
            {
                _imageSource = value;
                PropertyChanging("ImageSource");
            }
        }

        public void LoadImageFromFileSystem(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog
                          {
                              Filter = "Png Images (*.png)|*.png|JPEG Images (*.jpg)|*.jpg"
                          };
            if (!((bool)dlg.ShowDialog())) return;
            Stream s = dlg.File.OpenRead();
            using (s)
            {
                var bmi = new BitmapImage();
                bmi.SetSource(s);
                ImageSource = new WriteableBitmap(bmi);
            }
            ImageSource.Invalidate();
        }

        private static int[] _imageData;
        private int _imageWidth;
        private int _imageHeight;
        [DataMember]
        public int ImageHeight
        {
            get { return _imageHeight; }
            set { _imageHeight = value; }
        }
        [DataMember]
        public int ImageWidth
        {
            get { return _imageWidth; }
            set { _imageWidth = value; }
        }
        
        public static int[] ImageData
        {
            get { return  _imageData; }
            set { _imageData = value; }
        }

        internal override void PreSerializeActions()
        {
            _imageData = this.ImageSource.Pixels;
            _imageWidth = this.ImageSource.PixelWidth;
            _imageHeight = this.ImageSource.PixelHeight;
            base.PreSerializeActions();
        }

        internal override void PostDeserializeActions()
        {
            ImageSource = new WriteableBitmap(ImageWidth, ImageHeight);
            ImageData.CopyTo(this.ImageSource.Pixels,0);
            this.ImageSource.Invalidate();
            base.PostDeserializeActions();
        }

        internal override XElement GenerateXaml(XElement element)
        {
            var el = new XElement("Image",
                new XAttribute("Source", "{Binding ImageSource}"),
                new XAttribute("Stretch", ImageStretch));

            //pass it to base view models for additional attributes
            var finishedXaml = base.GenerateXaml(el);

            element.Add(new XComment("You will need to replace the 'Source' attribute of this Image with an actual Uri source for your image resource, or databind to it"));
            //add it to the root xaml
            element.Add(finishedXaml);
            return element;
        }
    }
}
